
package com.productpricemiddlemodule.dao;
import com.productpricemiddlemodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbProductPriceMiddleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbProductPriceMiddle tbProductPriceMiddle=null;
    private List<TbProductPriceMiddle> tbProductPriceMiddleList=null;

    public boolean add(TbProductPriceMiddle inTbProductPriceMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_ProductPriceMiddle(ProductId,PriceId,UserId) values(?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbProductPriceMiddle.getProductId());
        pt.setInt(2,inTbProductPriceMiddle.getPriceId());
        pt.setInt(3,inTbProductPriceMiddle.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public boolean del(int inProductPriceMiddleId){
        return UtilDAO.del("Tb_ProductPriceMiddle", "ProductPriceMiddleId", inProductPriceMiddleId);
    }
    public boolean edit(TbProductPriceMiddle inTbProductPriceMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_ProductPriceMiddle set ProductPriceMiddleId=?,ProductId=?,PriceId=?,UserId=? where ProductPriceMiddleId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbProductPriceMiddle.getProductPriceMiddleId());
        pt.setInt(2,inTbProductPriceMiddle.getProductId());
        pt.setInt(3,inTbProductPriceMiddle.getPriceId());
        pt.setInt(4,inTbProductPriceMiddle.getUserId());
        pt.setInt(5,inTbProductPriceMiddle.getProductPriceMiddleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public TbProductPriceMiddle getTbProductPriceMiddleByProductPriceMiddleId(int inProductPriceMiddleId) {
        tbProductPriceMiddle=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductPriceMiddle where ProductPriceMiddleId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inProductPriceMiddleId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbProductPriceMiddle == null)tbProductPriceMiddle = getTbProductPriceMiddle(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbProductPriceMiddle;
    }
    public List<TbProductPriceMiddle> getTbProductPriceMiddleAll() {
        tbProductPriceMiddleList=new ArrayList<TbProductPriceMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductPriceMiddle";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductPriceMiddleList.add(getTbProductPriceMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbProductPriceMiddleList;
    }
    public List<TbProductPriceMiddle> getTbProductPriceMiddleAll(VskPage vp) {
        tbProductPriceMiddleList=new ArrayList<TbProductPriceMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductPriceMiddle";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_ProductPriceMiddle", "ProductPriceMiddleId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductPriceMiddleList.add(getTbProductPriceMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbProductPriceMiddleList;
    }
    public TbProductPriceMiddle getTbProductPriceMiddle(ResultSet rs) throws Exception{
        TbProductPriceMiddle tbProductPriceMiddle = new TbProductPriceMiddle();
        tbProductPriceMiddle.setProductPriceMiddleId(rs.getInt("ProductPriceMiddleId"));
        tbProductPriceMiddle.setProductId(rs.getInt("ProductId"));
        tbProductPriceMiddle.setPriceId(rs.getInt("PriceId"));
        tbProductPriceMiddle.setUserId(rs.getInt("UserId"));
        return tbProductPriceMiddle;
    }
}
